package com.softhis.rekrutacja.client.view;

import java.util.Date;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.softhis.rekrutacja.client.presenter.UserFormPresenter.UserFormViewInterface;
import com.softhis.rekrutacja.client.view.widgets.impl.MyGxtButton;
import com.softhis.rekrutacja.client.view.widgets.impl.MyGxtDateField;
import com.softhis.rekrutacja.client.view.widgets.impl.MyGxtTextField;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IButton;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IDateField;
import com.softhis.rekrutacja.client.view.widgets.interfaces.ITextField;
import com.softhis.rekrutacja.shared.dto.UserDTO;

/**
 * @author Sasak
 * 
 */
public class UserFormView extends ContentPanel implements UserFormViewInterface {

	private FormPanel userForm = new FormPanel();

	private MyGxtTextField loginField = new MyGxtTextField();
	private MyGxtTextField passwordField = new MyGxtTextField();
	private MyGxtTextField nameField = new MyGxtTextField();
	private MyGxtTextField lastNameField = new MyGxtTextField();
	private MyGxtDateField birthDateField = new MyGxtDateField();

	private MyGxtButton saveButton = new MyGxtButton("Zapisz");
	private MyGxtButton cancelButton = new MyGxtButton("Anuluj");
	private ToolBar toolbar = new ToolBar();

	public UserFormView() {
		this.setHeaderVisible(false);
		this.setAutoHeight(true);
		this.setAutoWidth(true);

		FormLayout formLayout = new FormLayout(FormPanel.LabelAlign.LEFT);
		formLayout.setDefaultWidth(400);

		this.userForm.setLayout(formLayout);
		this.userForm.setHeaderVisible(false);
		this.userForm.setAutoWidth(true);
		this.userForm.setAutoHeight(true);

		this.loginField.setFieldLabel("Login");
		this.loginField.setAllowBlank(false);
		this.loginField.getMessages().setBlankText("To pole jest wymagane");

		this.loginField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {
				;
				if (value.length() < 4)
					return "To pole musi zawierać przynajmniej 4 znaki";

				if (value.length() > 30)
					return "To pole nie może być dłuższe niż 30 znaków";

				return null;
			}
		});

		this.passwordField.setPassword(true);
		this.passwordField.setFieldLabel("Hasło");
		this.passwordField.setAllowBlank(false);
		this.passwordField.getMessages().setBlankText("To pole jest wymagane");
		this.passwordField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {
				if (value.length() < 6)
					return "To pole musi zawierać przynajmniej 6 znaków";

				if (value.length() > 30)
					return "To pole nie może być dłuższe niż 30 znaków";

				return null;
			}
		});

		this.nameField.setFieldLabel("Imię");
		this.nameField.setAllowBlank(false);
		this.nameField.getMessages().setBlankText("To pole jest wymagane");
		this.nameField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {
				if (value.length() < 3)
					return "To pole musi zawierać przynajmniej 3 znaków";

				if (value.length() > 30)
					return "To pole nie może być dłuższe niż 30 znaków";

				return null;
			}
		});

		this.lastNameField.setFieldLabel("Nazwisko");
		this.lastNameField.setAllowBlank(false);
		this.lastNameField.getMessages().setBlankText("To pole jest wymagane");
		this.lastNameField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {
				if (value.length() < 3)
					return "To pole musi zawierać przynajmniej 3 znaków";

				if (value.length() > 30)
					return "To pole nie może być dłuższe niż 30 znaków";

				return null;
			}
		});

		this.birthDateField.setFieldLabel("Data urodzenia");
		this.birthDateField.setAllowBlank(false);
		this.birthDateField.getMessages().setBlankText("To pole jest wymagane");
		this.birthDateField.setFormatValue(true);
		this.birthDateField.setPropertyEditor(new DateTimePropertyEditor("dd-MM-yyyy"));
		this.birthDateField.setEmptyText("dd-mm-yyyy");
		this.birthDateField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {

				Date date = null;
				try {
					date = (Date) field.getPropertyEditor().convertStringValue(value);
				} catch (Exception ignored) {
				}
				if (date == null) {
					return "Podano nieprawidłowy format daty, wymagny format to : dd-mm-yyyy";	
				}

				if (date.after(new Date())) {
					return "Witaj przybyszu z przyszłości ;), nie można wprowadzić daty przyszłej";
				}
					
				return null;
			}
		});
		// this.birthDateField.setRegex("[0-9]{2}-[0-9]{2}-[0-9]{4}");

		this.toolbar.setAlignment(HorizontalAlignment.CENTER);
		this.toolbar.setWidth(520);
		this.saveButton.setWidth(125);
		this.cancelButton.setWidth(125);

		this.toolbar.add(this.saveButton);
		this.toolbar.add(this.cancelButton);

		this.userForm.add(this.loginField);
		this.userForm.add(this.passwordField);
		this.userForm.add(this.nameField);
		this.userForm.add(this.lastNameField);
		this.userForm.add(this.birthDateField);

		this.userForm.add(this.toolbar);

		FormButtonBinding formBind = new FormButtonBinding(this.userForm);
		formBind.addButton(this.saveButton);

		this.add(this.userForm);
	}

	public UserDTO getUserData() {
		UserDTO result = new UserDTO();
		result.setLogin(this.loginField.getValue());
		result.setPassword(this.passwordField.getValue());
		result.setName(this.nameField.getValue());
		result.setLastName(this.lastNameField.getValue());
		result.setBirthDate(this.birthDateField.getValue());
		return result;
	}

	public void fillForm(UserDTO user) {
		this.loginField.setValue(user.getLogin());
		this.passwordField.setValue(user.getPassword());
		this.nameField.setValue(user.getName());
		this.lastNameField.setValue(user.getLastName());
		this.birthDateField.setValue(user.getBirthDate());
	}
	
	public void clearForm(){
		this.userForm.clear();
	}

	public IButton getSaveButton() {
		return this.saveButton;
	}

	public IButton getCancelButton() {
		return this.cancelButton;
	}

	public ITextField getLoginField() {
		return this.loginField;
	}

	public ITextField getPasswordField() {
		return this.passwordField;
	}

	public ITextField getNameField() {
		return this.nameField;
	}

	public ITextField getLastNameField() {
		return this.lastNameField;
	}

	public IDateField getBirthDateField() {
		return this.birthDateField;
	}
	
	public ToolBar getToolBar(){
		return this.toolbar;
	}
}
